﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class HinhThucHopDongListView : Form
    {
        private BindingList<HinhThucHopDong> bindingListHinhThucHopDong;
        private HinhThucHopDong _hinhThucHopdong;

        public HinhThucHopDongListView()
        {
            InitializeComponent();
        }

        private void HinhThucHopDongListView_Load(object sender, EventArgs e)
        {
            string[] listFielName = new string[] { "MaQuanLy", "TenHinhThucHopDong", "CoThoiHan", "SoThang" };
            string[] listColumnName = new string[] { "Mã Quản Lý", "Tên Hình Thức Hợp Đồng", "Có Thời Hạn", "Số Tháng" };
            AppGridView.InitGridView(gvHinhThucHopDong, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gvHinhThucHopDong);
            AppGridView.ShowField(gvHinhThucHopDong, listFielName, listColumnName);

            LoadData();
        }
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _hinhThucHopdong = new HinhThucHopDong();
            OpenDetailForm();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteRow();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void gcHinhThucHopDong_DoubleClick(object sender, EventArgs e)
        {
            _hinhThucHopdong = gvHinhThucHopDong.GetFocusedRow() as HinhThucHopDong;
            if (_hinhThucHopdong.Oid > 0)
                OpenDetailForm();
        }
        void LoadData()
        {
            bindingListHinhThucHopDong = new BindingList<HinhThucHopDong>(DataAccessObject.HinhThucHopDong.GetListHinhThucHopDong());

            bindingSourceHinhThucHopDong.DataSource = bindingListHinhThucHopDong;
            gvHinhThucHopDong.Invalidate();
        }
        void OpenDetailForm()
        {
            HinhThucHopDongDetailView detailView = new HinhThucHopDongDetailView(_hinhThucHopdong);
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.DeleteChanged += new EventHandler(detailView_DeleteChanged);
            detailView.MdiParent = this.MdiParent;
            detailView.Show();
        }
        void DeleteRow()
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Xóa một dòng thông tin học kỳ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _hinhThucHopdong = gvHinhThucHopDong.GetFocusedRow() as HinhThucHopDong;
                if (_hinhThucHopdong != null && _hinhThucHopdong.Oid > 0)
                {
                    if (DataAccessObject.HinhThucHopDong.Delete(_hinhThucHopdong) > 0)
                    {
                        gvHinhThucHopDong.DeleteSelectedRows();
                        gvHinhThucHopDong.Invalidate();
                    }
                    else
                        MessageBox.Show("Không xóa được", "Có Lỗi Xảy Ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        void detailView_DeleteChanged(object sender, EventArgs e)
        {
            DeleteRow();
        }

        void detailView_SaveChanged(object sender, EventArgs e)
        {
                bindingListHinhThucHopDong.Add(_hinhThucHopdong);
            gvHinhThucHopDong.Invalidate();
        }


    }
}
